package com.qixin.total.controller;


import com.qixin.api.bean.ComUser;

import com.qixin.api.bean.ResultPage;
import com.qixin.api.bean.VoPage;
import com.qixin.api.bean.pojo.WelcomCount;
import com.qixin.api.service.ComUserService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;

import java.sql.SQLException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2018-10-25
 */
@Controller
@RequestMapping("/comUser")
public class ComUserController {
    @Autowired
    private ComUserService comUserService;

    @RequestMapping("/login.html")
    public String login() {
        return "login";
    }

    @RequestMapping("/regist.html")
    public String regist() {
        return "regist";
    }

    @RequestMapping("/index.html")
    public String welcome() {
        return "totalIndex";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public boolean login(ComUser comUser, HttpSession session) {
        System.out.println(comUser);
        if (comUserService.login(comUser, session)) {

            return true;
        }
        return false;
    }

    private static final Logger logger = LoggerFactory.getLogger(ComUserController.class);
    @Autowired
    ComUserController comUserController;

    //获取手机验证码
    @RequestMapping("/SendSMS")
    @ResponseBody
    public String SendSMS(Long userPhone) {
        return comUserService.SendSMS(userPhone);

    }

    //验证码校验  //// TODO: 2018/10/2 跳转页面model记得传手机号 用于设置缓存区
    @RequestMapping("/equalsCache")
    @ResponseBody
    public boolean equalsCache(String code, String callPhone, ComUser comUser, String param) {
        if (comUserService.equalsCache(code, callPhone) == true) {

            return comUserService.registerUser(comUser, param);

        } else {
            System.out.print("验证码不符");
            return false;
        }


    }

    //跳转 首页
    @RequestMapping("/findMenu")
    @ResponseBody
    public ComUser findMenu(HttpSession session) {
        ComUser user = new ComUser();
        user = (ComUser) session.getAttribute("user");
        return user;
    }

    //跳转系统管理的人员管理页面

    @RequestMapping("/userAdmin")
    public String userAdmin(ComUser comUser, HttpSession session) {
        return "/power/user";
    }

    //系统管理 查询所有用户信息 包括用户的角色
    @RequestMapping("/userAdminList")
    @ResponseBody
    public ResultPage<ComUser> userAdminList(VoPage voPage, ComUser comUser) {
        ResultPage<ComUser> resultPage = comUserService.findUserAndRole(voPage, comUser);
        return resultPage;
    }

    //删除用户
    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(Long id) {
        try {
            return comUserService.deleteUser(id);
        } catch (SQLException e) {
            return "删除失败";
        }
    }

    //跳转编辑用户
    @RequestMapping("/updateUser.html")
    public String ToupdateUser() {
        return "/power/user_add";
    }


    /**
     * 首页查询板块个数
     *
     * @return
     */
    @RequestMapping("/findWelcomCount")
    @ResponseBody
    public WelcomCount findWelcomCount() {
        return comUserService.findWelcomCount();
    }
}